// pages/list-1/list-1.js
var app = getApp()
import {catdata,detailapi,addcarapi,appnavapi} from "../../utils/api"
import utils from "../../utils/util"
Page({
  data: {
    hidedata:false,//必抢的显示状态
    tasteid:0,//商品规格id
    num:1,//请求页数
    classdata:[],//分类数据
    catdata:[],//分类商品数据
    len:10,//判断数据上栏加载是否还有数据请求
    name:{},//商店名字
    classindex:0, //左边分类切换
    class_id:"",
    detail:[],//详情页的数据
    totalnum:"",//库存数量
    good:{
        goods_id:"",//商品的id
        car_num:1,//购买的数量
        sku_id:""//规格的id
    }
  },
  async onLoad(){
    //   底部切换函数
    
    var data = {
        class_id:this.data.class_id,
        page_size:10,
        page:this.data.num
      }
    //   底部导航的数据
    const res =await appnavapi()
    var arr = res.data.data
    if (res.data.code==200) {
        for (let i = 0; i < arr.foot_navs.length; i++) {
            arr.foot_navs[i].active = true
        }
        console.log(arr);
        app.globalData.tabBar = arr
        //  分类页面的数据
        const res1= await catdata(data)      
        if (res1.data.code == 200) {
        wx.hideLoading()
        console.log(res1.data.data.class[0].class_id);
        var len = res1.data.data.goods.length
        this.setData({
            classdata:res1.data.data.class,
            catdata:res1.data.data.goods,
            len:len,
            name:res1.data.data.mch,
            class_id:res1.data.data.class[0].class_id
            })
        } else {
        wx.hideLoading()
        utils.wxshowtoast("数据请求失败")
        this.setData({
                
            })
        }
    } else {
        utils.wxshowtoast("数据请求失败")
    }
    app.editTabBar()
   
  },

  //分类页面上拉加载   
  catmuch(){
      if(this.data.len===10){
        let data = {
            class_id:this.data.class_id,
            page_size:10,
            page:++this.data.num
          }
          console.log(data);
          this.catfn(data)
      }
      
  },
  //左边分类切换
  tabclass(e){
    this.setData({
        classindex:e.currentTarget.dataset.index,
        class_id:e.currentTarget.dataset.classid,
        num:1,
        catdata:[]
    })
    let data = {
        class_id:e.currentTarget.dataset.classid,
        page_size:10,
        page:1
      }
      console.log(data);
      this.catfn(data)
  },
//   分类页面数据请求
   async catfn(data){
    const res = await catdata(data)
    wx.hideLoading()
    console.log(res.data.data)            
    if (res.data.code == 200) {
    var len = res.data.data.goods.length
    this.setData({
        catdata:[...this.data.catdata,...res.data.data.goods],
        len:len
        })
    } else {
    
    utils.wxshowtoast("数据请求失败")
    this.setData({
            
        })
    }
    },
  // 购物数量加减
  addnum(e){
    var num= this.data.good.car_num 
    if(e.currentTarget.dataset.num==0){
     if(num<=1){
       return utils.wxshowtoast("商品数量不能小于1")
     }
     num--
     this.data.good.car_num = num
      this.setData({
          good:this.data.good
      })
   }else{
     if(num>=this.data.totalnum){
      return utils.wxshowtoast("库存不足")
     }
     num++
     this.data.good.car_num = num
      this.setData({
          good:this.data.good
      })
   }
 },
 // 提交订单
 getorder(){
    if(wx.getStorageSync('openid')==""){
        console.log(0);
        wx.navigateTo({
            url: "/pages/login/login"
        })
        return 
    }
    if(this.data.totalnum<=0){
    return utils.wxshowtoast("库存不足")
    }
    var order = {
        goods_sku_id:this.data.good.sku_id,
        goods_num:this.data.good.car_num
    }
    wx.navigateTo({
        url: "/pages/order/order?order="+JSON.stringify(order)
    })
},
// 加入购物车
async addcar(){
    if(wx.getStorageSync('openid')==""){
        console.log(0);
        wx.navigateTo({
            url: "/pages/login/login"
        })
        return 
    }
    console.log(this.data.good);
    const res = await addcarapi(this.data.good)
    // console.log(res.data);
    if(res.data.code==200){
        console.log(0);
        wx.hideLoading()
        utils.wxshowtoast("成功加入购物车")
    }else if(res.data.code==0){
        console.log(0);
        wx.hideLoading()
        utils.wxshowtoast(res.data.msg)
    }else{
        utils.wxshowtoast(res.data.msg)
        wx.hideLoading()
    }
    
},
// 必抢的规格选择
  tastecative(e){
    console.log(e.currentTarget.dataset.index,e.currentTarget.dataset.skuid,e.currentTarget.dataset.totalnum)
    this.data.good.sku_id = e.currentTarget.dataset.skuid
    this.data.good.car_num = 1
    this.setData({
        good:this.data.good,
        tasteid:e.currentTarget.dataset.index,
        totalnum:e.currentTarget.dataset.totalnum
    })
    console.log(this.data.good,this.data.totalnum);
  },
  //必抢的显示
 async show(e){
    console.log(e.currentTarget.dataset.id)
    this.data.good.goods_id = e.currentTarget.dataset.id
    this.setData({
        good:this.data.good,
        hidedata:true
    })
    const res  = await detailapi({goods_id:e.currentTarget.dataset.id})
    if(res.data.code==200){
        wx.hideLoading()
        var array = res.data.data.sku
        for (let i = 0; i < array.length; i++) {
            array[i].spec_show = array[i].spec_show.join("-");
        }
        this.data.good.sku_id = array[0].goods_sku_id
        this.setData({
            detail:res.data.data,
            good:this.data.good,
            totalnum:array[0].goods_sku_stock
        })
        console.log(this.data.good,this.data.totalnum);
    }else{
        wx.hideLoading()
        utils.wxshowtoast("数据请求失败")
        this.setData({
            detail:[]
        })
    }
  },
  //必抢的隐藏
  hide(){
    this.data.good.car_num = 1
    this.setData({
        good:this.data.good,
        hidedata:false
    })
  },
  //店铺跳转
  indexurl(){
    wx.reLaunch({
      url: "/pages/index/index"
  })
  },
  // 商品详情页
  detail(e){
    console.log(e.currentTarget.dataset.path, e.currentTarget.dataset.owning)
    if (e.currentTarget.dataset.owning === 1) {
        wx.navigateTo({
            url: "/pages/detail/detail?id=" +e.currentTarget.dataset.id
        })
    } else {
        wx.navigateToMiniProgram({
            appId: 'wx32540bd863b27570',
            path: e.currentTarget.dataset.path
        })
    }
  },
  
  // 生命周期函数--监听页面初次渲染完成
  onReady: function () {
   
  },
  /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})  